package com.cpit.qualitycontrol.web.controller;

import com.cpit.platform.common.constant.LogConstant;
import com.cpit.platform.common.entity.Result;
import com.cpit.platform.common.utils.LogUtils;
import com.cpit.platform.dto.OperLog;
import com.cpit.platform.dto.Organization;
import com.cpit.platform.dto.User;
import com.cpit.platform.log.LogOperator;
import com.cpit.platform.service.OperLogService;
import com.cpit.platform.service.OrganizationService;
import com.cpit.platform.service.UserService;
import com.cpit.qualitycontrol.entity.ProjectOrganization;
import com.cpit.qualitycontrol.entity.SubTask;
import com.cpit.qualitycontrol.service.ProjectService;
import com.cpit.qualitycontrol.service.SubTaskService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/QcOrganization")
public class QcOrganizationController {
    private String moduleNmae = "机构：";
    @Autowired
    private UserService userService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private SubTaskService subTaskService;
    @Autowired
    private OperLogService operLogService;

    private final String loggerName = this.getClass().getName();

    @ApiOperation(value = "删除组织关系项目", notes = "在系统中删除已经存在的组织关系项目，" +
            "如果该组织关系被组织关系引用，则不允许删除。")
    @ApiImplicitParam(name = "data", value = "组织关系描述对象。", required = true, dataType = "Organization")
    @ResponseBody
    @PreAuthorize("hasAuthority('deleteOrganization')")
    @RequestMapping(value = "/deleteOrganization", method = RequestMethod.DELETE)
    public Result<Organization> deleteOrganization(@RequestBody Organization data) {
        try {
            List<User> users = userService.findAllByOrganizationId(data.getId());
//            List<ProjectOrganization> pr = projectService.findByOrganizationId(data.getId());
            List<SubTask> st = subTaskService.findByOrganizationTeamId(new Long(data.getId()));

            if (users != null && users.size() > 0) {
                LogOperator.info(loggerName, "该组织机构下存在用户信息，不能删除:" + data.getId());
                throw new Exception("该组织机构下存在用户信息，不能删除");
            }

//            if (pr != null && pr.size() > 0) {
//                LogOperator.info(loggerName, "该组织机构下存在项目信息，不能删除:" + data.getId());
//                throw new Exception("该组织机构下存在项目信息，不能删除");
//            }

            if (st != null && st.size() > 0) {
                LogOperator.info(loggerName, "该组织机构下存在任务信息，不能删除:" + data.getId());
                throw new Exception("该组织机构下存在任务信息，不能删除");
            }
            organizationService.delete(data);
            writeLog(LogConstant.OPER_DELETE, Result.CODE_SUCCESS, data.getName());
            return new Result<Organization>(Result.CODE_SUCCESS, "", data);
        } catch (Exception e) {
            writeLog(LogConstant.OPER_DELETE, Result.CODE_FAILED, e.getMessage());
            return new Result<Organization>(Result.CODE_FAILED, e.getMessage(), data);
        }
    }

    private void writeLog(String op, String result, String detail) {
        OperLog operLog = LogUtils.createOperLog(LogConstant.MODULE_CONFIG, op, result, moduleNmae + detail);
        operLogService.writeLog(operLog);
    }
}
